package top.v5it.japi.plus.common.func;

import java.util.Objects;
import java.util.function.Consumer;

/**
 * 自定义函数接口
 *
 * @author zhanpu
 * @date 2018/12/18
 */
@FunctionalInterface
public interface VoidFunc1<T, E extends Throwable> {


    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     * @throws E
     */
    void accept(T t) throws E;

    /**
     * 处理往外抛异常
     *
     * @param after
     * @return
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);

        return t -> {
            try {
                accept(t);
                after.accept(t);
            } catch (Throwable e) {
                throw new FuncException(e);
            }
        };
    }

    /**
     * 基于本函数返回{@link FuncException}
     *
     * @param t 参数
     */
    default void acceptWithFuncException(T t) {
        try {
            accept(t);
        } catch (Throwable e) {
            throw new FuncException(e);
        }
    }
}

